//角色编辑
var groupid;
var iconid;
var subid;
var menuid;
var id;
var defaultVal = null;
$(function() {
  groupid = ly.getUrlParam("groupid");
  iconid = ly.getUrlParam("iconid");
  subid = ly.getUrlParam("subid");
  menuid = ly.getUrlParam("menuid");
  var isCreat = ly.getUrlParam("isCreat"); //1：新增  2：编辑

  $(".whiteboard").remove();

  if (isCreat == 2) {
    $("#thisName").html("编辑");
    id = ly.getUrlParam("id");
    $("#thisId").val(id);
    $(".getFom").getFormTwo({
      roleId: id,
      type: 'post'
    }, function(data) {
      /*$("#roleAffiliate").attr("ly-val",data.roleAffiliate);*/
      //角色归属
      // $("#roleAffiliate").getSelect({
      // 	type:"radio",
      // 	asName:"roleAffiliate",
      // 	code:'roleAffiliate',	
      // 	default:data.roleAffiliate
      // });
      $("#attr1").getSelect({
        type: "radio",
        asName: "attr1",
        list: [
          { value: "0", display: "禁用" },
          { value: "1", display: "启用" }
        ],
        default: data.attr1
      });
      //getSelection();
      if (data.extendRoleId != 0 && data.extendRoleId != null && data.extendRoleId != '') {
        defaultVal = data.extendRoleId;
        var url = site.apiUrl + "/back/role/selectRoleIdFindMenuAndFunction";
        requestPost(url, { 'id': data.extendRoleId }, selectRoleIdFindMenuAndFunction, 'post', false, false);

      } else {
        $("#roleCopy").addClass("hide");
      }

      //查询所有角色
      // var url = site.apiUrl + "/back/role/selectAllRoleList";
      // requestPost(url, { 'id': id }, selectAllRole, 'post', false, false);
    });
    //initFom(id);			
  } else {
    $("#thisName").html("新增");
    $("#roleCopy").addClass("hide");
    //状态
    $("#attr1").getSelect({
      type: "radio",
      asName: "attr1",
      list: [
        { value: "0", display: "禁用" },
        { value: "1", display: "启用" }
      ],
      default: 1
    });
    //角色归属
    // $("#roleAffiliate").getSelect({
    // 	type:"radio",
    // 	asName:"roleAffiliate",
    // 	code:'roleAffiliate',	
    // 	default:1
    // });

    //查询所有角色
    // var url = site.apiUrl + "/back/role/selectAllRoleList";
    // requestPost(url, { 'id': id }, selectAllRole, 'post', false, false);
  }


  //保存
  $("body").on("click", "#save-btn", function(event) {
    event.preventDefault();
    var success = $('#form1').validate('submitValidate');
    if (success) {
      rolepackageObj(isCreat);
    }
  });


});

function save(isCreat) {
  var obj = role_packageObj(isCreat);
  if (obj != null && obj != '') {
    if (isCreat == 1) { //新增
      var url = site.apiUrl + "/roleManager/addRoInfo";
      ajaxForm(url, obj, submitCallback, 'POST', false, false);
    } else { //修改
      var url = site.apiUrl + "/roleManager/updateDetailRoInfo";
      ajaxForm(url, obj, submitCallback_success_update, 'POST', false, false);
    }
  }
}
//编辑成功
function submitCallback_success_update(data) {
  if (data.code == 200) {
    ly.alert("修改成功", function() {
      ly.goto("role.html");
    });

  } else {
    ly.toast(data.msg);
  }
}
//新增成功
function submitCallback(data) {
  if (data.code == 200) {
    ly.alert("新增成功", function() {
      ly.goto("role.html");
    });
  } else {
    ly.toast(data.msg);
  }
}



//保存
function submitFomCallback(data) {
  if (data.code == 200) {
    ly.alert('保存成功', function() {
      // window.localStorage.setItem();
      ly.goto("wx-service-wxServiceJobFair.html");
    });
  } else {
    ly.toast(data.msg);
  }
}
//修改
function updateFomCallback(data) {
  if (data.code == 200) {
    ly.alert('修改成功', function() {
      // window.localStorage.setItem();
      ly.goto("wx-service-wxServiceJobFair.html");
    });
  } else {
    ly.toast(data.msg);
  }
}

/**
 * 打包数据
 */
function rolepackageObj(type) {
  var extendRoleId = $("select[name='extendRoleId'] option:checked").val(); //继承角色
  var roleName = $("#roleName").val(); //角色名称
  var roleDesc = $("#roleDesc").val(); //角色介绍
  var attr1 = $("#attr1 input:checked").val(); //当前状态
  // var roleAffiliate = $("#roleAffiliate input:checked").val(); //角色归属
  // var fileName = $('#roleIcon input').attr('filename');
  // var filePath = $('#roleIcon input').attr('value');
  // var fileType = $('#roleIcon input').attr('filetype');
  // var fileSize = $('#roleIcon input').attr('filesize');

  var obj = "";
  var createUserId = window.sessionStorage.getItem("backstage-access-id");
  // var f = { //角色图标
  //   'fileName': fileName,
  //   'filePath': filePath,
  //   //'fileType':fileType,
  //   'fileSize': fileSize
  // };
  var url = "";
  if (type == 1) { //新增
    url = site.apiUrl + "/roleManager/addRoInfo";
    obj = {
      "roleName": roleName,
      "roleDesc": roleDesc,
      "createUserId": createUserId,
      'attr1': attr1,
      // 'roleAffiliate': roleAffiliate,
      'extendRoleId': extendRoleId,
    };
  } else {
    var thisId = $("#thisId").val();
    url = site.apiUrl + "/roleManager/updateDetailRoInfo";
    obj = {
      "roleName": roleName,
      "roleDesc": roleDesc,
      'attr1': attr1,
      // 'roleAffiliate': roleAffiliate,
      'extendRoleId': extendRoleId,
      "updateUserId": createUserId,
      "id": thisId
    };
  }
  // obj.fileupload = f;
  console.log(obj);
  request({
    url: url,
    data: obj,
    type: "post",
    ajaxform: 'true',
    callback: function(f) {
      console.log(f);
      if (f.code == 200) {
        if (type == 1) {
          ly.alert('保存成功', function() {
            ly.goto("role.html");
          });
        } else {
          ly.alert('修改成功', function() {
            ly.goto("role.html");
          });
        }
      } else {
        ly.toast(f.msg);
      }
    }
  });

  return obj;
}

function selectAllRole(data) {
  if (data.code == 200) {
    var list = jsonArrangement(data.result, "roleName|id", "display|value");
    list.push({ "value": '', "display": '请选择' });
    $("#extendRoleId").getSelectM({
      type: "select",
      asName: "extendRoleId",
      list: list,
      default: defaultVal == null ? "" : defaultVal
    }, function(data) { //extendRoleId
      $(data.selector).on("change", function() {
        var extendRoleId = $(this).find("option:checked").val();
        if (extendRoleId != null && extendRoleId != '') {
          $("#roleCopy").removeClass("hide");
          var url = site.apiUrl + "/roleManager/selectRoleThisMenuAndFuncByRoleId";
          requestPost(url, { 'id': extendRoleId }, selectRoleIdFindMenuAndFunction, 'post', false, false);
        } else {
          $("#roleCopy").addClass("hide");
        }

      });
    });

  } else {
    ly.toast(data.msg);
  }

  defaultVal = null;


}

function selectRoleIdFindMenuAndFunction(data) {

  console.log(data);
  if (data.code == "success") {
    // $("#funNum").html("功能：" + data.result.funNum);
    $("#menuNum").html("菜单：" + data.result.menuNum);
  } else {
    ly.toast(data.msg);
  }

}